package cc.vicp.djx314.starfood.util.impls;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.domain.dish.ProvideTime;
import cc.vicp.djx314.starfood.domain.order.OrderDish;
import cc.vicp.djx314.starfood.util.DaysOfWeekTran;
import cc.vicp.djx314.starfood.util.DishTimeEnsure;
import cc.vicp.djx314.starfood.util.exception.StartProvideTimeLaterThanEndException;

public class DishTimeEnsureImpl implements DishTimeEnsure {
	@Autowired
	DaysOfWeekTran daysOfWeekTran;

	@Override
	public boolean isDishTimeSupport(Dish dish, Date date) throws StartProvideTimeLaterThanEndException {
		Assert.notNull(date, "判定菜式是否可以订购的日期不能为空");
		List<ProvideTime> times = dish.getProvideTimes();
		boolean isSupport = false;
		Calendar pandingTime = timeToCalendarFixedDay(date);  //改变了星期数的calendar
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);  //没有改变星期数的calendar
		for (ProvideTime time : times) {
			Time startTime = time.getStartTime();
			Time endTime = time.getEndTime();
			Assert.notNull(startTime, "判定菜式是否可以订购的菜式可以订购开始时间不能为空");
			Assert.notNull(endTime, "判定菜式是否可以订购的菜式可以订购结束时间不能为空");
			Date startDate = new Date(startTime.getTime());
			Date endDate = new Date(endTime.getTime());
			Calendar startCal = timeToCalendarFixedDay(startDate);
			Calendar endCal = timeToCalendarFixedDay(endDate);
			if (startCal.after(endCal)) {
				throw new StartProvideTimeLaterThanEndException("开始时间" + startCal
						+ "大于结束时间" + endCal);
			}
			//System.out.println("====真正星期数:" + daysOfWeekTran.weekDayToCalendar((time.getDayOfWeek())) + "====");
			//System.out.println("====期望星期数:" + dateCal.get(Calendar.DAY_OF_WEEK) + "====");
			//System.out.println("====" + Calendar.SUNDAY + "====");
			if (daysOfWeekTran.weekDayToCalendar((time.getDayOfWeek()))
					.equals(dateCal.get(Calendar.DAY_OF_WEEK))) {
				//System.out.println(time.getStartTime() + "    " + time.getEndTime());
				//System.out.println((Integer)(time.getDayOfWeek() % 7) + "     " + dateCal.get(Calendar.DAY_OF_WEEK));
				//System.out.println("====开始时间:" + startTime + "====");
				//System.out.println("====结束时间:" + endTime + "====");
				//System.out.println("====菜式时间:" + pandingTime + "====");
				if (startCal.before(pandingTime) && endCal.after(pandingTime)) {
					isSupport = true;
					break;
				}
			}
		}
		return isSupport;
	}

	private Calendar timeToCalendarFixedDay(Date date) {
		Calendar pandingTime = Calendar.getInstance();
		pandingTime.setTime(date);
		pandingTime.set(2222, 2, 22);
		return pandingTime;
	}

	@Override
	public List<OrderDish> getSupportInfo(List<OrderDish> orderDishes, Date date) throws StartProvideTimeLaterThanEndException {
		List<OrderDish> resultOrderDishes = null;
		if (orderDishes != null) {
			for (OrderDish orderDish : orderDishes) {
				Dish dish = orderDish.getDish();
				boolean isSupport = isDishTimeSupport(dish, date);
				orderDish.setNowEnabled(isSupport);
			}
			resultOrderDishes = orderDishes;
		}
		return resultOrderDishes;
	}
}